package com.srs.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.srs.model.Group;
import com.srs.util.HibernateUtil;

public class GroupDao {

	final static Logger logger = LoggerFactory.getLogger(GroupDao.class);

	private List<Group> grouplist;
	private Group group;

	public void delete(Integer groupId) {

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			group = (Group) session.get(Group.class, groupId);
			session.delete(group);
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Group> list() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			grouplist = session.createQuery("from Group").list();
			return grouplist;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}

	public Group get(Integer groupId) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from Group g where g.groupId=:groupId");
			q.setInteger("groupId", groupId);
			group = (Group) q.uniqueResult();
			if (group==null) {
				return new Group();
			}
			return group;
		} finally {
			session.close();
		}

	}

	public Boolean nameIsUsed(String groupName) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from Group g where g.groupName=:groupName");
			q.setString("groupName", groupName);
			grouplist = q.list();
			if (grouplist.size()==0) {
				return false;
			}
			return true;
		} finally {
			session.close();
		}

	}	
	
	public void insert(Group group) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(group);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public void update(Group group) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(group);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}
	
	public static void main(String[] args) {
		// Init sessionFactory
		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
		HibernateUtil.setSessionFactory(sessionFactory);
		
		// Get list params
		GroupDao dao = new GroupDao();
		List<Group> groups = dao.list();
		System.out.println(groups);
	}
}
